package com.yami.shop.multishop.controller;


import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.AirdropTask;
import com.yami.shop.bean.model.AirdropTaskConditions;
import com.yami.shop.bean.model.User;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.multishop.util.SecurityUtils;
import com.yami.shop.service.AirdropTaskConditionsService;
import com.yami.shop.service.AirdropTaskService;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (TzAirdropTask)表控制层
 *
 * @author makejava
 * @since 2023-05-22 18:29:21
 */
@RestController
@RequestMapping("/admin/airdropTask")
public class AirdropTaskController {
    /**
     * 服务对象
     */
    @Resource
    private AirdropTaskService airdropTaskService;

    @Resource
    private AirdropTaskConditionsService airdropTaskConditionsService;

    /**
     * 分页查询所有数据
     *
     * @param page        分页对象
     * @param airdropTask 查询实体
     * @return 所有数据
     */
    @GetMapping("/page")
    public ServerResponseEntity<IPage<AirdropTask>> page(PageParam<AirdropTask> page, @ParameterObject AirdropTask airdropTask) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        airdropTask.setDropShopId(shopId);
        IPage<AirdropTask> airdropTaskList = airdropTaskService.page(page, airdropTask);
        return ServerResponseEntity.success(airdropTaskList);
    }

    /**
     * 通过任务主键查询空投数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ServerResponseEntity<AirdropTask> selectOne(@PathVariable Serializable id) {

        return ServerResponseEntity.success(this.airdropTaskService.getById(id));
    }

    /**
     * 新增数据
     * 不指定条件则空投全部有效会员
     *
     * @param airdropTask 实体对象
     * @return 新增结果
     */
    @PostMapping
    public ServerResponseEntity<Boolean> insert(@RequestBody AirdropTask airdropTask) {
        airdropTask.setAirdropTaskId(IdUtil.getSnowflakeNextId());
        airdropTask.setDropShopId(SecurityUtils.getShopUser().getShopId());
        airdropTask.setStatus(0);
        airdropTask.setCreateTime(new Date());
        this.airdropTaskService.save(airdropTask);
        if (!airdropTask.getAirdropTaskConditions().isEmpty()) {
            airdropTask.getAirdropTaskConditions().forEach(airdropTaskConditions -> {
                airdropTaskConditions.setTaskId(airdropTask.getAirdropTaskId());
                airdropTaskConditionsService.save(airdropTaskConditions);
            });
        }
        return ServerResponseEntity.success();
    }

    /**
     * 修改数据
     *
     * @param airdropTask 实体对象
     * @return 修改结果
     */
    @PutMapping
    public ServerResponseEntity<Boolean> update(@RequestBody AirdropTask airdropTask) {
        return ServerResponseEntity.success(this.airdropTaskService.updateById(airdropTask));
    }

    /**
     * 立即执行空投任务
     *
     * @param airdropTaskId 空投任务Id
     * @return 修改结果
     */
    @PutMapping("/executeImmediately/{airdropTaskId}")
    public ServerResponseEntity<Boolean> executeImmediately(@PathVariable("airdropTaskId") Long airdropTaskId) {
        airdropTaskService.airdropTask(airdropTaskId);
        return ServerResponseEntity.success();
    }

    /**
     * 批量取消
     *
     * @param idList 主键集合
     * @return 删除结果
     */
    @PutMapping("/batchCancellation")
    public ServerResponseEntity<Boolean> batchCancellation(@RequestBody List<Long> idList) {
        List<AirdropTask> airdropTasks = new ArrayList<>();
        idList.forEach(id -> {
            AirdropTask airdropTask = new AirdropTask();
            airdropTask.setAirdropTaskId(id);
            airdropTask.setStatus(3);
            airdropTasks.add(airdropTask);
        });
        return ServerResponseEntity.success(this.airdropTaskService.updateBatchById(airdropTasks));
    }

    /**
     * 查看空投人员
     *
     * @param airdropTask 空投条件集合、商品信息
     * @return 删除结果
     */
    @PostMapping("/dropUserList")
    public ServerResponseEntity<List<User>> dropUserList(@RequestBody AirdropTask airdropTask) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        return ServerResponseEntity.success(airdropTaskService.getDropUserInfoList(airdropTask.getAirdropTaskConditions(),airdropTask.getArticleIds(),airdropTask.getArticleType(),shopId));
    }
}

